#include "World.h"

bool hasEnding (std::string const &fullString, std::string const &ending)
{
    if (fullString.length() >= ending.length()) {
        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}
static void key(const Event * theEvent, void * data){
	((World*)data)->Process(theEvent);
}
void World::InitInput()
{
	std::string line;
	vector<std::string> control;
	std::ifstream myfile ("Control.nnf");
	  if (myfile.is_open())
	  {
		while ( myfile.good() )
		{
		  getline (myfile,line);
		}
		myfile.close();
	  }
	  control = split(line,',');
	for(int j=0;j<control.size();j++)
	{
		if(control[j] == "wheel"){
			control[j].append("_up");
			nout << control[j] << endl;
			window->get_panda_framework()->define_key(control[j],control[j],key,this);
			control[j] = control[j].substr(0, control[j].size()-3);
			control[j].append("_down");
			window->get_panda_framework()->define_key(control[j],control[j],key,this);
			continue;
		}
		window->get_panda_framework()->define_key(control[j],control[j],key,this); 
		control[j].append("-up");
		window->get_panda_framework()->define_key(control[j],control[j],key,this);
	}
}
void World::Process(const Event * theEvent)
{
	std::string name = theEvent->get_name();
	nout << name << endl;
	if(name == "wheel_up" || name == "wheel_down"){
		once_down_keys.insert(name);
		return;
	}
	if(hasEnding(name,"up"))
	{
		name = name.substr(0, name.size()-3);
		this->Remove(name,&down_keys);
	}else{
		down_keys.insert(name);
	}
}
LVector2 windowCentre(WindowFramework *w)
{
	LVector2 size;
size.set_x(w->get_graphics_window()->get_properties().get_x_size()/2);
size.set_y(w->get_graphics_window()->get_properties().get_y_size()/2);
return size;
}
bool World::check_input()
{
	//Check Mouse
	mouseWatcher = (MouseWatcher*)window->get_mouse().node();
	if (mouseWatcher->has_mouse()){//relative because it resets itself every frame
		try{
			mx =window->get_graphics_window()->get_pointer(0).get_x();
			my = window->get_graphics_window()->get_pointer(0).get_y();
			mx -= windowCentre(window).get_x();
			my -= windowCentre(window).get_y();
			window->get_graphics_window()->move_pointer(0,windowCentre(window).get_x(), windowCentre(window).get_y());
		}catch(...){
			//do nothing--if the mouse leaves the window do nothing.
		}
	}
	if(down_keys.find("escape") != down_keys.end())
		return false;
	return true;
}
bool World::Remove(std::string Event,std::set<std::string>* n){
		std::set<std::string>::iterator it = n->find(Event);
		if (it != n->end()){
		  n->erase(it);
		  return true;
		}
		return false;
}